
#include <asm.h>

.global C_SYMBOL(SetColorARGB32)

//void SetColorARGB32(Pixel* pDst, int dstX, int dstY, int width, int height,
//                    int dstPitch, Pixel color, unsigned char srcOpacity);

C_SYMBOL(SetColorARGB32):
height = 0x0;
dstPitch = 0x4;
color = 0x8;
srcOpacity = 0xC;

    MOV     R12, SP
    STMFD   SP!, {R4-R12,LR}

    LDR     R4, [R12,#dstPitch]

    MUL     R5, R4, R2

    ADD     R1, R5, R1, LSL#2
    ADD     R1, R0, R1

    LDR     R4, [R12,#height]
    LDR     R5, [R12,#color]
    LDR     R6, [R12,#srcOpacity]
    LDR     R2, [R12,#dstPitch]

//    MOV    R4, R3                       //height
//    MOV    R3, R2                       //width
//    MOV    R2, R1                       //dstPitch
//    MOV    R1, R0                       //pDst

    AND     R6, R6, #0xFF
    CMP     R6, #0xFF
    BLT     _Start_T_

    MOV R3, R3, LSL #2
    SUB	R2, R2,	R3
    TMCRR   wR0, R5, R5

_lc010_000413_:
    MOV	R5, R3
    ADD	R6, R1,	#7
    BIC	R6, R6,	#7
    SUB	R6, R6,	R1
    CMP	R6, #0
    BEQ	_lc030_000440_

_lc020_000428_:
    CMP	R6, R5
    MOVGT	R6, R5
    SUB	R5, R5,	R6

_lc021_000432_:
    SUBS	R6, R6,	#4
    WSTRW   wR0, [R1], #4
    BGT	_lc021_000432_
    CMP	R5, #0
    BEQ	_lc060_000464_

_lc030_000440_:
    SUBS	R5, R5,	#0x20
    ADDMI	R6, R5,	#0x20
    ADDMI	R5, R5,	#0x20
    BMI	_lc020_000428_

_lc040_000449_:
    WSTRD   wR0, [R1], #8
    WSTRD   wR0, [R1], #8
    SUBS	R5, R5,	#0x20
    WSTRD   wR0, [R1], #8
    WSTRD   wR0, [R1], #8
    BGE	_lc040_000449_
    ADDS	R5, R5,	#0x20
    MOVGT	R6, R5
    BGT	_lc020_000428_

_lc060_000464_:
    ADD	R1, R1,	R2
    SUBS	R4, R4,	#1
    BGT	_lc010_000413_

    LDMFD   SP, {R4-R11,SP,PC}

_Start_T_:
    CMP   R6, #0
    LDMEQFD   SP, {R4-R11,SP,PC}

    TMCRR   wR13, R5, R5

	MOV R10, R1
	MOV R12, R2
	MOV R7,  R4
	MOV R4,  R3


    ORR R6, R6, R6, LSL #16
    TMCRR   wR14, R6, R6

	MOV LR, R4, LSL #2

    LDR     R4, = 0x00FF00FF
    TMCRR   wR15, R4, R4

    LDR     R4, = 0x00000008
    TMCR    wCGR3, R4

LoopLine:
	MOV	R3, LR
	MOV	R2, R10
	B Alpha_8u_C4ARow
Next:
	SUB	R7, R7,	#1
	ADD	R10, R10, R12
	CMP	R7, #0
	BHI	LoopLine

End:

    LDMFD   SP, {R4-R11,SP,PC}



Alpha_8u_C4ARow:


    ADD	R6, R2,	#7
    BIC	R6, R6,	#7
    SUB	R6, R6,	R2
    CMP	R6, #0
    BEQ	_Aligned8_

_Process_One_:

    CMP	R6, R3
    WLDRW   wR6, [R2]

    MOVGT	R6, R3
    SUB	R3, R3,	R6

_Loop_One_:
//.long 0xe7fddefe
    SUBS	R6, R6,	#4
    WMOV    wR5,wR14
    WUNPCKELUB wR0, wR13
    WUNPCKELUB wR1, wR6
    WMULUL  wR0, wR0, wR5
    WSUBH   wR7,wR15,wR5
    WMULUL  wR1, wR1, wR7

    WADDH     wR0, wR0, wR1
    WLDRWGT   wR6, [R2, #4]
    WSRLHG    wR0, wR0, wCGR3

    WPACKHUS wR0, wR0, wR1

    //ADD R2,R2,#4
    WSTRW   wR0, [R2], #4

    BGT	_Loop_One_

    CMP	R3, #0
    BEQ     Next

_Aligned8_:
    SUBS	R3, R3,	#0x10
    ADDMI	R6, R3,	#0x10
    ADDMI	R3, R3,	#0x10
    BMI	_Process_One_


    WLDRD   wR4, [R2]
    WLDRD   wR5, [R2, #8]

_Loop_Aligned8_:

    SUBS	R3, R3,	#0x10
    PLD	[R2,#0x30]

    WUNPCKELUB wR6, wR13

    WMOV    wR7, wR14
    WUNPCKELUB wR8, wR4

    WMULUL  wR6, wR6, wR7
    WSUBH   wR7,wR15,wR7
    WMULUL  wR8, wR8, wR7


    WUNPCKEHUB wR9, wR13

    WMOV    wR10,wR14

    WADDH     wR6, wR6, wR8
    WUNPCKEHUB wR11, wR4
    WSRLHG  wR6, wR6, wCGR3

    WMULUL  wR9, wR9, wR10
    WSUBH   wR10,wR15,wR10
    WMULUL  wR11, wR11, wR10

    WADDH   wR9, wR9, wR11
    WSRLHG  wR9, wR9, wCGR3

    WPACKHUS wR6, wR6, wR9
    WSTRD   wR6, [R2], #8


    WUNPCKELUB wR6, wR13

    WMOV    wR7,wR14
    WUNPCKELUB wR8, wR5
    WMULUL  wR6, wR6, wR7

    WSUBH   wR7,wR15,wR7
    WMULUL  wR8, wR8, wR7

    WLDRDGE wR4, [R2, #8]

    WADDH     wR6, wR6, wR8
    WSRLHG    wR6, wR6, wCGR3


    WUNPCKEHUB wR9, wR13

    WMOV    wR10,wR14
    WUNPCKEHUB wR11, wR5

    WMULUL  wR9, wR9, wR10

    WSUBH   wR10,wR15,wR10
    WMULUL  wR11, wR11, wR10
    WADDH   wR9, wR9, wR11
    WSRLHG  wR9, wR9, wCGR3
    WPACKHUS wR6, wR6, wR9
    WLDRDGE wR5, [R2, #16]
    WSTRD wR6, [R2], #8

    BGE	_Loop_Aligned8_
    ADDS	R3, R3,	#0x10
    BEQ     Next
    MOVGT	R6, R3
    //SUBGT   R2, R2, #8
    BGT	_Process_One_
